package wt.hpad.dialog;

import android.app.Activity;
import android.os.Build;

import java.util.Timer;
import java.util.TimerTask;

import cn.pedant.SweetAlert.SweetAlertDialog;
import wt.hpad.R;
import wt.hpad.utils.DialogUtil;

/**
 * Created by wt on 1.8.
 */

public class TimeLoadingDialog {
    private SimpleDialogHandler mHandler;
    private long mStartTime;
    private Activity mActivity;
    private String mStartContent;
    private String mEndContent;
    private String mFailContent;
    private SweetAlertDialog mDialog;

    public TimeLoadingDialog(Activity activity, String startContent, String endContent, String failContent) {
        this.mActivity = activity;
        mStartContent = startContent;
        mEndContent = endContent;
        mFailContent = failContent;
    }

    public TimeLoadingDialog(Builder builder) {
        mActivity = builder.mActivity;
        mStartContent = builder.mStartContent;
        mEndContent = builder.mEndContent;
        mFailContent = builder.mFailContent;
        mHandler = builder.mHandler;
    }


    public void start() {
        mStartTime = System.currentTimeMillis();
        mDialog = new SweetAlertDialog(mActivity, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(mStartContent);
        mDialog.setCancelable(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mDialog.getProgressHelper().setBarColor(mActivity.getResources().getColor(R.color.material_deep_teal_50, null));
        }
        if (!mDialog.isShowing()) {
            mDialog.show();
            // 3.5s后自动关闭
            new Timer().schedule(new ExceptionTask(), 3500);
        }
    }

    public void end() {
        long duration = System.currentTimeMillis() - mStartTime;
        try {
            if (duration < 1000) {
                Thread.sleep(1000 - duration);
            }
        } catch (InterruptedException e) {
            mHandler.onConfirm();
            mDialog.dismiss();
            e.printStackTrace();
        }
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            DialogUtil.showInToast(mActivity, mEndContent);
            try {
                this.finalize();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

    private class ExceptionTask extends TimerTask {
        @Override
        public void run() {
            if (mDialog.isShowing()) {
                mDialog.dismiss();
                mHandler.onCancel();
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SweetAlertDialog dialog = new SweetAlertDialog(mActivity, SweetAlertDialog.PROGRESS_TYPE);
                        dialog.setTitleText("错误")
                                .setContentText(mFailContent)
                                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
                        dialog.show();
                    }
                });
            }
        }
    }

    public static class Builder {
        private Activity mActivity;
        private String mStartContent;
        private String mEndContent;
        private String mFailContent;
        private SimpleDialogHandler mHandler;


        public Builder setActivity(Activity activity) {
            this.mActivity = activity;
            return this;
        }

        public Builder setStartContent(String content) {
            this.mStartContent = content;
            return this;
        }

        public Builder setEndContent(String content) {
            this.mEndContent = content;
            return this;
        }

        public Builder setfailContent(String content) {
            this.mFailContent = content;
            return this;
        }


        public Builder setHandler(SimpleDialogHandler handler) {
            this.mHandler = handler;
            return this;
        }

        public TimeLoadingDialog build() {
            return new TimeLoadingDialog(this);
        }
    }

}
